package mars.activity;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import bear.download.HttpsUtils;
import bear.download.ULread;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class sendActivity extends Activity{
	private Spinner choose;
	private Button send1,emtry1;
	private EditText msgcon;
	String selected;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.send);
		send1 = (Button)findViewById(R.id.send1);
		emtry1 = (Button)findViewById(R.id.emtry1);
		choose=(Spinner)findViewById(R.id.choose);
		msgcon=(EditText)findViewById(R.id.content1);
		//接收人列表
		String userlist[] = ULread.rename1.split("\\$");
		List <String>list=new ArrayList<String>();
		list.add(getString(R.string.please));
		for(int i=0;i<ULread.n;i++){
		list.add(userlist[i]);
		}
	    ArrayAdapter<String> adapter =new ArrayAdapter<String>(sendActivity.this,R.layout.item,R.id.textViewId,list);
	    choose.setAdapter(adapter);
	    choose.setOnItemSelectedListener(new SpinnerOnItemSelectedListener());
	    //接收人显示
	    emtry1.setOnClickListener(new Button.OnClickListener()
        {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				msgcon.setText("");
			}	
        });
	    //发送按钮
	    send1.setOnClickListener(new Button.OnClickListener()
        {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				HttpsUtils setpost=new HttpsUtils();
				String postData = "username=" + landActivity.user.getText().toString() + "&touser="+selected+ "&hash=" + landActivity.hash + "&content=" + URLEncoder.encode(msgcon.getText().toString());
				setpost.post("https://www.loushiapp.com/?c=pm&a=postpm",postData);
				msgcon.setText("");
				Toast.makeText(sendActivity.this, R.string.sended,Toast.LENGTH_LONG).show();
			}	
        });
	}
	//接收人字符串存入selected
	class SpinnerOnItemSelectedListener implements OnItemSelectedListener { 
	    public void onItemSelected (AdapterView<?> adapterView, View view, int position, long logicId){ 
	     selected=adapterView.getItemAtPosition(position).toString();
	    }
	    public void onNothingSelected (AdapterView<?> arg0) {  
	    	System.exit(0);
	    	}
	}
}
